//==========================
// - FileName:      	EventCenter.cs         
// - Created:         	Wu	
// - CreateTime:    	2023-04-24 11:01:52	
// - Email:         	1479136868@qq.com		
// - Region:        		
// - Description:   
//==========================
using System;
using System.Collections.Generic;
namespace Framework
{
    public static class EventCenter
    {
        private static Dictionary<EventType, Action> _events = new Dictionary<EventType, Action>();

        private static Dictionary<EventType, Action<object[]>> _argEvents = new Dictionary<EventType, Action<object[]>>();
        public static void AddListener(EventType eventType, Action action)
        {
            if (_events.ContainsKey(eventType))
            {
                _events[eventType] = _events[eventType] + action;
                return;
            }
            _events.Add(eventType, action);
        }

        public static void AddListener(EventType eventType, Action<object[]> action)
        {
            if (_argEvents.ContainsKey(eventType))
            {
                _argEvents[eventType] = _argEvents[eventType] + action;
                return;
            }
            _argEvents.Add(eventType, action);
        }

        public static void RemoveListener(EventType eventType, Action action)
        {
            if (_events.ContainsKey(eventType))
            {
                _events[eventType] = _events[eventType] - action;
                if (_events[eventType] == null)
                {
                    _events.Remove(eventType);
                }
            }
        }
        public static void RemoveListener(EventType eventType, Action<object[]> action)
        {
            if (_argEvents.ContainsKey(eventType))
            {
                _argEvents[eventType] = _argEvents[eventType] - action;
                if (_argEvents[eventType] == null)
                {
                    _argEvents.Remove(eventType);
                }
            }
        }

        public static void Send(EventType eventType, params object[] args)
        {
            if (_events.ContainsKey(eventType))
            {
                Delegate delegater = _events[eventType];
                if (delegater != null)
                {
                    Delegate[] invocationList = delegater.GetInvocationList();
                    int count = invocationList.Length;
                    for (int i = 0; i < count; i++)
                    {
                        invocationList[i].DynamicInvoke();
                    }
                }
            }
            if (_argEvents.ContainsKey(eventType))
            {
                Delegate delegater = _argEvents[eventType];
                if (delegater != null)
                {
                    Delegate[] invocationList = delegater.GetInvocationList();
                    int count = invocationList.Length;
                    for (int i = 0; i < count; i++)
                    {
                        invocationList[i].DynamicInvoke(new[] { args });
                    }
                }
            }
        }
    }
}